#!/usr/bin/env python
#encoding=utf-8

import os
import urllib
import threading
from util import download_file

class ChapterDownloadThread(threading.Thread):
    '''章节下载线程'''
    def __init__(self, resource, save_path):
        threading.Thread.__init__(self)
        self.resource = resource
        self.save_path = save_path
        self.setName(resource['filename'])
        pass

    def download(self):
        resource_url_list = self.resource['resource']
        filefullname = self.save_path + '/' + self.resource['filename']
        if not os.path.exists(filefullname):
            for resource_url in resource_url_list:
                if download_file(resource_url, filefullname):
                    break
                print('Try next resource.')
        pass

    def run(self):
        self.download()
